<?php
/**
 * Introduction: APP商品列表页面
 * @author: 杨陈鹏
 * @date: 2016/4/18 12:00
 * @email: yangchenpeng@cdlinglu.com
 */

namespace WeChat\Controller\Goods;


use Think\Controller;

class IndexController extends Controller
{
    /*
     ** @introduction: 分类ID获取商品列表
     ** @author: 杨陈鹏
     ** @date: 2015-04-12 55:37
     ** @email: yangchenpeng@cdlinglu.com
     */
    public function lists()
    {

        //通过分类获取商品列表
        $category_id = intval(I('post.category_id'));

        $price = I('post.price');

        $page = intval(I('post.page', 1));


        $order = I('post.order');

        $keywords = trim(I('post.keywords'));


        $goodsLogic = D('Common/Goods', 'Logic');


        $categoryLogic = D('Common/category', 'Logic');


        $fields = 'id,title,thumb,description,goods_salenum';


        $where = "goods_state = 1 AND  publish_time < " . NOW_TIME;

        if ($price['min'] && $price['max']) {

            $where .= " AND (goods_price > " . $price['min'] . " AND goods_price < " . $price['max'] . ")";

        } elseif ($price['min'] && !$price['max']) {

            $where .= " AND goods_price > " . $price['min'];

        } elseif (!$price['min'] && $price['max']) {

            $where .= " AND goods_price < " . $price['max'];

        }

        //如果存在关键词
        if ($keywords)
            //$where .= " AND (title like '%$keywords%' or keywords like '%$keywords%' or description like '%$keywords%' or content like '%$keywords%' or product_data like '%$keywords%' or spec like '%$keywords%')";
            $where .= " AND `title` like '%$keywords%'";

        //如果有分类ID
        if ($category_id) {

            //获取指定分类下的所有子分类ID

            $category_ids = $categoryLogic::getChilds($category_id, 'id');

            $category_ids_str = '';
            foreach ($category_ids as $v)
                $category_ids_str .= $v['id'] . ',';

            $category_ids_str = $category_ids_str . $category_id;

            $category_ids_str = rtrim($category_ids_str, ',');

            $where .= " AND category_id IN($category_ids_str)";

        }

        //获取数据
        $data = $goodsLogic::getData($where, $page, $fields, $order);

        outPut($data);


    }
    /*
     ** @introduction: 通过搜索关键词获取商品列表
     ** @author: 杨陈鹏
     ** @date: 2015-04-12 55:37
     ** @email: yangchenpeng@cdlinglu.com
     */
//    public function keywordsToLists()
//    {
//
//
//        $page = intval(I('post.page',1));
//        $order = intval(I('post.order',1));
//        $keywords = trim(I('post.keywords'));
//
//        if(!$keywords)
//            outPut(['code' => 300,'msg' => '请输入查询关键词!']);
//
////        $where['title'] = array('like','%'.$keywords.'%');
////        $where['keywords'] = array('like','%'.$keywords.'%');
////        $where['description'] = array('like','%'.$keywords.'%');
////        $where['content'] = array('like','%'.$keywords.'%');
////        $where['product_data'] = array('like','%'.$keywords.'%');
////        $where['spec'] = array('like','%'.$keywords.'%');
////        $where['_logic'] = 'or';
////        $where['goods_state'] = array('eq',1);
////        $where['publish_time'] = array('lt', NOW_TIME);
//
//        $fields = 'id,title,thumb,description';
//
//
//        $logic = D('Common/Goods', 'Logic');
//        //获取数据
//        $data = $logic::getData($where,$page,$fields,$order);
//
//        outPut($data);
//
//
//
//    }
    /*
    ** @introduction: 获取商品详情
    ** @author: 杨陈鹏
    ** @date: 2015-04-12 55:37
    ** @email: yangchenpeng@cdlinglu.com
    */
    public function detail()
    {

        $goods_id = intval(I('post.goods_id'));

        if (!$goods_id)
            outPut(['code' => 300, 'msg' => '网络错误,请稍候再试!']);

        $logic = D('Common/Goods', 'Logic');


        $data = $logic::getOneData(['id' => $goods_id]);

        outPut($data);

    }

    /*
    ** @introduction: 根据商品规格获取商品信息
    ** @author: 杨陈鹏
    ** @date: 2015-04-12 55:37
    ** @email: yangchenpeng@cdlinglu.com
    */
    public function getSpec()
    {

        $goods_id = intval(I('post.goods_id'));
        $goods_attr_val_ids = I('post.goods_attr_val_ids');

        if (!$goods_id || !$goods_attr_val_ids)
            outPut(['code' => 300, 'msg' => '网络错误,请稍候再试!']);

        $logic = D('Common/Goods', 'Logic');

        $data = $logic::getSpec(['goods_id' => $goods_id, 'goods_attr_val_ids' => $goods_attr_val_ids]);

        outPut($data);

    }

    /*
    ** @introduction: 获取商品运费
    ** @author: 杨陈鹏
    ** @date: 2015-04-12 55:37
    ** @email: yangchenpeng@cdlinglu.com
    */
    public function freight()
    {


        $logic = D('Common/Goods', 'Logic');

        $data = I('post.data');

        outPut($logic::freight($data), true);

    }

    /*
    ** @introduction: 商品点赞
    ** @author: 杨陈鹏
    ** @email: yangchenpeng@cdlinglu.com
    */

/*

var data = {

    data:{

        goods_id:34,

    }

}
*/
    public function like()
    {


        $data = I('post.data');

        $logic = D('Common/Goods', 'Logic');

        outPut($logic::updateGoodsStatus(intval($data['goods_id']),'`like`'));

    }

    /*
    ** @introduction: 商品浏览量
    ** @author: 杨陈鹏
    ** @email: yangchenpeng@cdlinglu.com
    */

/*

var data = {

    data:{

        goods_id:34,

    }

}
*/
    public function views()
    {


        $data = I('post.data');

        $logic = D('Common/Goods', 'Logic');

        outPut($logic::updateGoodsStatus(intval($data['goods_id']),'views'));

    }

}